<?php
declare (strict_types = 1);

namespace app\command;

use app\model\order\Orders;
use app\model\product\product\StoreProduct;
use app\model\statistics\StaticStoreProductTotal;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class StoreProductStatic extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('storeProductStatic')
            ->setDescription('每天运行一次的定时任务');
    }

    /**
     * @param Input $input
     * @param Output $output
     * @return int|void|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function execute(Input $input, Output $output)
    {
        $info = StaticStoreProductTotal::where('static_date', date('Y-m-d'))->where('type','store_product_everyday_total')->find();
        $count = StoreProduct::where('is_show', 1)->where('is_del',0)->count();

        $data = [
            'number' => $count ?? 1,
            'type' => 'store_product_everyday_total',
            'static_date' => date('Y-m-d'),
            'add_time' => time(),
        ];
        if($info){
            StaticStoreProductTotal::where('id',$info['id'])->update($data);
        }else{
            StaticStoreProductTotal::create($data);
        }
        // 指令输出
        $output->writeln('app\command\StoreProductStatic');
    }
}
